/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.netbeans.test.db.derby;

import java.io.File;
import java.sql.Connection;
import junit.framework.Test;
import org.netbeans.modules.derby.StartAction;
import org.netbeans.junit.NbModuleSuite;
import org.netbeans.modules.derby.DerbyOptions;
import org.netbeans.modules.derby.StopAction;
import org.netbeans.test.db.util.DbUtil;
import org.openide.util.actions.SystemAction;

/**
 *
 * @author lg198683
 */
public class DerbyDatabaseTest extends DbJellyTestCase {
      
    public DerbyDatabaseTest(String s) {
        super(s);
    }
    
    @Override
    public void setUp() {
        getDataDir().mkdirs();
        DerbyOptions.getDefault().setSystemHome(getDataDir().getAbsolutePath());
        String derbyLoc = System.getProperty("derby.location");
        if (derbyLoc == null || derbyLoc.length() == 0) {
            derbyLoc = getLocationInJDK(System.getProperty("java.home")).getAbsolutePath();
        }
        DerbyOptions.getDefault().setLocation(derbyLoc);
    }
    
    private static File getLocationInJDK(String javaHome) {
        File dir = new File(javaHome);
        assert dir != null && dir.exists() && dir.isDirectory() : "java.home is directory";
        // path to JavaDB in JDK6
        File loc = new File(dir.getParentFile(), "db"); // NOI18N
        return loc != null && loc.exists() && loc.isDirectory() ? loc : null;
    }

    public void testStartAction() {
       StartAction start=SystemAction.get(StartAction.class);
       start.performAction();
       // we need a support to detect that the DB started see: #112788
       sleep(15000);
    }
    
    public void testStopAction() throws Exception {
        StopAction stop=SystemAction.get(StopAction.class);
        stop.performAction();
    }
    
    public void testConnect() throws Exception{
        SystemAction.get(StartAction.class).performAction();
        sleep(2000);

        String url="jdbc:derby://localhost:1527/testdbtest;create=true";
        Connection con=DbUtil.createDerbyConnection(url);
        con.close();
   }
    
   public static Test suite() {
        return NbModuleSuite.create(NbModuleSuite.createConfiguration(DerbyDatabaseTest.class)
                .addTest("testStartAction", "testConnect", "testStopAction")
                .enableModules(".*")
                .clusters(".*")
                );
    }
}
